package com.ksl.kinglockguard.domain;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class H5List implements Serializable {
	/**
	 * 
	 */
	private int mCount;
	private List<H5News> h5List = new ArrayList<H5News>();

	public int getMinID() {
		if(h5List == null)
			return 0;
		return h5List.get(getH5Count() - 1).getId();
	}

	public int getMaxID() {
		if(h5List == null)
			return 0;
		int maxId = h5List.get(0).getId();
		for (int i = 1; i < h5List.size(); i++) {
			int tmpId = h5List.get(i).getId();
			if (tmpId > maxId) {
				maxId = tmpId;
			}
		}
		return maxId;
	}
	
	public int getH5Count() {
		return mCount;
	}

	public List<H5News> getNewslist() {
		return h5List;
	}

	public static H5List parse(JSONArray dataList) throws IOException,
			JSONException {
		H5List mlist = new H5List();
		if (null != dataList) {
			mlist.mCount = dataList.length();
			for (int i = 0; i < dataList.length(); i++) {
				JSONObject newsJson = dataList.getJSONObject(i);
                Log.d("lijc", "H5List ------ items: " + newsJson);

				H5News news = new H5News();

                setId(newsJson, news);
                setTitle(newsJson, news);
                setSummary(newsJson, news);
                setArticleUrl(newsJson, news);

                setFromUrl(newsJson, news);
                setSlide2Browser(newsJson, news);

                //图片相关字段
                setLargeImageUrl(newsJson, news);
                setMiddleImageUrl(newsJson, news);
                setMiddleImageWidth(newsJson, news);
                setMiddleImageHeight(newsJson, news);
                setSmallImageUrl(newsJson, news);
                setShowSmallImage(newsJson, news);

                // H5游戏特有的字段
                setEngineType(newsJson, news);
                setPlayersNumber(newsJson, news);
                setCpToken(newsJson, news);
                setGameToken(newsJson, news);
                setGameFlag(newsJson, news);
                setIconImgUrl(newsJson, news);
                setDeskIconFlag(newsJson, news);
                setGameName(newsJson, news);
                //add by lijc 2015-05-06 start
                //egret 新版本增加
                setEgret_runtime_updateUrl(newsJson, news);
                setGame_platformType(newsJson, news);
                setGame_ServerUrl(newsJson, news);
                setGame_platInfoParam(newsJson, news);
              //add by lijc 2015-05-06 end
                
                //add by lijc 2015-05-29 start
                setsetGame_screen_direction(newsJson, news);
                //add by lijc 2015-05-29 end
                
                Log.d("lijc","h5news item: " + news);
                mlist.h5List.add(news);
			}
            mlist.mCount = mlist.h5List.size();
        }
        return mlist;
	}
	private static void setsetGame_screen_direction(JSONObject newsJson, H5News news) throws JSONException {
		if( hasField(newsJson, "play_game_screen_direction") ){
			news.setPlay_game_screen_direction(newsJson.getString("play_game_screen_direction"));
		}
	}

	private static void setGame_platInfoParam(JSONObject newsJson, H5News news) throws JSONException {
		if( hasField(newsJson, "game_platInfoParam") ){
			news.setGame_platInfoParam(newsJson.getString("game_platInfoParam"));
		}
	}
	
	private static void setGame_ServerUrl(JSONObject newsJson, H5News news) throws JSONException {
		if( hasField(newsJson, "game_ServerUrl") ){
			news.setGame_ServerUrl(newsJson.getString("game_ServerUrl"));
		}
	}
	
	private static void setGame_platformType(JSONObject newsJson, H5News news) throws JSONException {
		if( hasField(newsJson, "game_platformType") ){
			news.setGame_platformType(newsJson.getString("game_platformType"));
		}
	}
	
	private static void setEgret_runtime_updateUrl(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "egret_runtime_updateUrl") ){
            news.setEgret_runtime_updateUrl(newsJson.getString("egret_runtime_updateUrl"));
        }
    }
	
    private static void setSlide2Browser(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "slide2browser") ){
            news.setSlide2browser(newsJson.getInt("slide2browser"));
        }
    }

    private static void setShowSmallImage(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "show_small_image") ){
            news.setSlide2browser(newsJson.getInt("show_small_image"));
        }
    }

    private static void setSmallImageUrl(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "small_img_url") ){
            news.setSmallImageUrl(newsJson.getString("small_img_url"));
        }
    }

    private static void setLargeImageUrl(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "large_img_url") ){
            news.setLargeImageUrl(newsJson.getString("large_img_url"));
        }
    }

    private static void setMiddleImageHeight(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "middle_img_height") ){
            news.setMiddleImageHeight(newsJson.getInt("middle_img_height"));
        }
    }

    private static void setMiddleImageWidth(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "middle_img_width") ){
            news.setMiddleImageWidth(newsJson.getInt("middle_img_width"));
        }
    }

    private static void setMiddleImageUrl(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "middle_img_url") ){
            news.setMiddleImageUrl(newsJson.getString("middle_img_url"));
        }
    }

    private static void setGameToken(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "game_id") ){
            news.setGameToken(newsJson.getString("game_id"));
        }
    }

    private static void setCpToken(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "game_cp") ){
            news.setCpToken(newsJson.getString("game_cp"));
        }
    }

    private static void setEngineType(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "game_engine") ){
            news.setEngineType(newsJson.getInt("game_engine"));
        }
    }

    private static void setGameFlag(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "game_flag") ){
            news.setGameFlag(newsJson.getInt("game_flag"));
        }
    }
    
    private static void setPlayersNumber(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "game_players") ){
            news.setPlayersNumber(newsJson.getInt("game_players"));
        }
    }

    private static void setFromUrl(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "from_url") ){
            news.setFromUrl(newsJson.getString("from_url"));
        }
    }

    private static void setArticleUrl(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "url") ){
            news.setArticleUrl(newsJson.getString("url"));
        }
    }

    private static void setSummary(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "summary") ){
            news.setSummary(newsJson.getString("summary"));
        }
    }

    private static void setTitle(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "title") ){
            news.setTitle(newsJson.getString("title"));
        }
    }


    private static void setId(JSONObject newsJson, H5News news) throws JSONException {
        if( hasField(newsJson, "id") ) {
            news.setId(newsJson.getInt("id"));
        }
    }

    private static void setIconImgUrl(JSONObject newsJson, H5News news) throws JSONException {
    	if( hasField(newsJson, "icon_img_url") ) {
    		news.setIconUrl(newsJson.getString("icon_img_url"));
    	}
    }
    private static void setGameName(JSONObject newsJson, H5News news) throws JSONException {
    	if( hasField(newsJson, "game_name") ) {
    		news.setGameName(newsJson.getString("game_name"));
    	}
    }
    private static void setDeskIconFlag(JSONObject newsJson, H5News news) throws JSONException {
    	if( hasField(newsJson, "generate_desktop_icon") ) {
    		news.setGenerateDeskIconFlag(newsJson.getInt("generate_desktop_icon"));
    	}
    }
    
    private static boolean hasField(JSONObject newsJson, String field) {
        if(newsJson.isNull(field)) {
            return false;
        }
        return true;
    }
}
